<?php if (!defined('THINK_PATH')) exit();?><!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"/>
    <title>长乐美食城</title>
    <link rel="stylesheet" type="text/css" href="/Public/assets/home/images/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/Public/vendor/slick/slick.css"/>
    <link rel="stylesheet" type="text/css" href="/Public/vendor/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" type="text/css" href="/Public/assets/home/css/common.css"/>
    
</head>
<body>

<div class="logo">
    <h1><a href="index.html"><img class="img-responsive" src="/Public/assets/home/images/favicon.ico"
                                  alt="长乐美食城"/></a></h1>

    <h3>长乐美食城</h3>

    <div class="clears"></div>
</div>


    <h3 style="width: 50%;
    text-align: center;
    line-height: 30px;
    color: #fff;
    font-weight: normal;
    background-color: #c06090;margin-top: 10px;">我的购物车</h3>
    <form>
        <section class="cart">
            <?php if(is_array($carts)): $i = 0; $__LIST__ = $carts;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$cart): $mod = ($i % 2 );++$i;?><dl class="good" data-cart="<?php echo ($cart["cart_id"]); ?>">
                    <dt><img class="img-responsive"
                             src="<?php echo ($cart["thumb"]); ?>"
                             alt="产品图片">
                    </dt>
                    <dd class="dd">
                        <ul>
                            <li><?php echo ($cart["name"]); ?></li>
                            <li><span>¥</span> <span
                                    class="price"><?php echo ($cart["price"]); ?>元</span></li>
                            <li>
                                <div class="btn-group">
                                    <span class="sub">－</span>
                                    <input type="text" value="<?php echo ($cart["num"]); ?>" class="num">
                                    <span class="add">＋</span>
                                </div>
                                <a class="btn-cart-omit del_product" href="javascript:void(0);">移除</a>
                            </li>
                        </ul>
                    </dd>
                </dl><?php endforeach; endif; else: echo "" ;endif; ?>
            <p id="total">总金额：<span>¥</span> <span
                    id="total-price"><?php echo (number_format($total,2)); ?>元</span></p>

            <input class="submit" type="submit" value="我要购买"/>
        </section>
    </form>


<?php if(($product_num) > "0"): ?><a href="<?php echo U('Cart/index');?>" id="btn-cart"><i class="fa fa-shopping-cart"></i><span><?php echo ($product_num); ?></span></a><?php endif; ?>
<a href="javascript:;" id="to_top" onclick="goTop();return false;">返回</a>

<div class="footer">
    <p>
        <a href="<?php echo U('Index/index');?>" class="aaaa">商城首页</a>
        <a href="<?php echo U('Cart/index');?>">购物车</a>
        <a href="<?php echo U('Customer/index');?>">会员中心</a>
    </p>

    <dl>
        <dt><a href="javascript:void(0);"><img class="img-responsive" src="/Public/assets/home/images/favicon.ico"
                                               alt="长乐美食城"/></a></dt>
        <dd><a href="javascript:void(0);">武汉长乐未央网络科技有限公司提供技术支持</a></dd>
    </dl>
</div>

<script type="text/javascript" src="/Public/assets/home/js/jquery.min.js"></script>
<script type="text/javascript" src="/Public/vendor/slick/slick.js"></script>
<script type="text/javascript" src="/Public/assets/home/js/index.js"></script>

    <script>
        $(function () {
            //移除商品
            $('.del_product').click(function () {
                if (confirm('确定要移除此商品么？')) {
                    var cartId = $(this).parents('dl').attr('data-cart');
                    var _this = $(this);
                    $.post("<?php echo U('remove_cart');?>", {cartId: cartId}, function () {
                        _this.parents('dl').remove();
                        window.location.reload();
                    });
                    return false;
                }
            });

            //增加数量
            $('.add').click(function () {
                var t = $(this).parent().find('input[class *= num]');
                t.val(parseInt(t.val()) + 1);
                var id = $(this).parents('dl').attr('data-cart');
                $.post("<?php echo U('add_num');?>", {
                    id: id
                });

                setTotal();
            });

            //减少数量
            $('.sub').click(function () {
                var t = $(this).parent().find('input[class *= num]');
                t.val(parseInt(t.val()) - 1);
                if (parseInt(t.val()) < 1) {
                    t.val(1);
                    return false;
                }
                var id = $(this).parents('dl').attr('data-cart');
                $.post("<?php echo U('sub_num');?>", {
                    id: id
                });

                setTotal();
            });

            //修改数量
            $('.num').blur(function () {
                var $input = $(this);
                var id = $(this).parent().attr('data-cartId');
                if (!/^\d+$/.test($input.val()) || $input.val() <= 0) {
                    $input.val(1);
                }
                var t_change = $input.val();
                var id = $(this).parents('dl').attr('data-cart');
                $.post("<?php echo U('change_num');?>", {
                    id: id,
                    num: t_change
                });

                setTotal();
            });


            //计算总价
            function setTotal() {
                var total = 0;
                $(".good").each(function () {
                    total += parseInt($(this).find('input[class *= num]').val()) *
                        parseFloat($(this).find('ul li span[class *= price]').text());
                });

                $('#total-price').text(total.toFixed(2));
            }

            $('.submit').click(function () {
                location.href = "<?php echo U('Order/index');?>";
                return false;
            })
        })
    </script>

</body>
</html>